# 仅供参考和学习,有建议的建议提出来
from selenium import webdriver
import time
import requests
import pprint
import easygui
driver = webdriver.Chrome()
content = easygui.enterbox('你想评论什么内容：')
zh = easygui.enterbox('请输入你的代码岛ID：')
pwd = easygui.enterbox('请输入你的代码岛密码：')
contentIdList = []
num = 0
offset = 0
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36',
    'Content-Type': 'application/json',
}
with open('offset.txt') as f:
    offsetI = int(f.read())
for a in range(offsetI, offsetI + 48, 24):
    print(a)
    data = '{"type":"list","data":{"limit":24,"offset":%s,"sort":1,"type":1,"isPublic":true,"filter":"latest"}}'%a
    response = requests.post('https://box3.codemao.cn/api/api/content-server-rpc', headers=headers, data=data)
    jsonResponse = response.json()
    for i in jsonResponse['data']['data']['rows']:
        pprint.pprint(i['content_id'])
        contentIdList.append('https://box3.codemao.cn/g/' + str(i['content_id']))
    offset = a
print(offset)
with open('offset.txt', 'w') as f:
    f.write(str(offset))

def signIn(UserId, PassWord):
    global num
    time.sleep(1)
    clickList = ['/html/body/div/header/div/div[2]/button', '/html/body/div/div/div[2]/div/div[2]/div[1]/div[1]/div[2]', '/html/body/div/div/div[2]/div/div[2]/div[1]/label/div/div/i/i']
    for i in clickList:
        time.sleep(1)
        driver.find_element_by_xpath(i).click()
    sendKeys = [UserId, PassWord]
    clickList2 = ['/html/body/div/div/div[2]/div/div[2]/div[1]/div[2]/div[1]/div[1]/input', '/html/body/div/div/div[2]/div/div[2]/div[1]/div[2]/div[2]/div[1]/input']
    for i in range(len(sendKeys)):
        time.sleep(1)
        eom = driver.find_element_by_xpath(clickList2[i])
        time.sleep(1)
        eom.send_keys(sendKeys[i])
    driver.find_element_by_xpath('/html/body/div/div/div[2]/div/div[2]/div[1]/div[2]/button').click()
    num = 1


def sendKey(i):
    time.sleep(1)
    driver.execute_script('window.scrollTo(0,500)')
    time.sleep(1)
    emo = driver.find_element_by_xpath('//*[@id="main"]/main/div[3]/div[1]/div/div[2]/div[1]/textarea')
    time.sleep(1)
    emo.send_keys(content)
    time.sleep(1)
    driver.find_element_by_xpath('//*[@id="main"]/main/div[3]/div[1]/div/div[2]/div[2]/button').click()
    with open('评论过的岛三.txt', 'a', encoding='utf-8') as f:
        f.write(str(i) + '\n')
    time.sleep(20)

for a in contentIdList:
    with open('评论过的岛三.txt') as f:
        wanDaoSan = f.readlines()
    for i in range(len(wanDaoSan)):
        if '\n' in wanDaoSan[i]:
            wanDaoSan[i] = wanDaoSan[i].replace('\n', '')
    if a not in wanDaoSan:
        url = a
        print(url)
        driver.get(url)
        if num != 1:
            signIn(zh, pwd)
        try:
            sendKey(a)
        except:
            print(0)
